package com.hddf.project.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.hddf.project.base.BaseDaoImpl;
import com.hddf.project.dao.CommentDao;
import com.hddf.project.pojos.Comment;
import com.hddf.project.util.O;

public class CommentDaoImpl extends BaseDaoImpl implements CommentDao {

	/**
	 * 根据项目ID查询批示
	 * 
	 * @param pid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Comment> listComment(Integer pid, Session s) {
		Query query = s.createQuery("from Comment where project_id=:temp order by id desc");
		query.setInteger("temp", pid);
		return query.list();
	}

	/**
	 * 根据项目ID查询批示数量
	 * 
	 * @param pid
	 * @return
	 */
	public Integer countComment(Integer pid, Session s) {
		Query query = s.createQuery("select count(id) from Comment where project_id=:temp ");
		query.setInteger("temp", pid);
		int result = 0;
		if (null != query.uniqueResult()) {
			result = Integer.parseInt(query.uniqueResult().toString());
		}
		return result;
	}

	/**
	 * 根据项目ID和用户ID查询批示内容
	 * 
	 * @param pid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Comment commentDetail(Integer userid, Integer pid, Session s) {
		Query query = s.createQuery("from Comment where project_id=:temp1 and user_id=:temp2 order by id desc");
		query.setInteger("temp1", pid);
		query.setInteger("temp2", userid);
		List<Comment> clist = (List<Comment>) query.list();
		Comment c = null;
		if (O.ckLt(clist)) {
			c = clist.get(0);
		}
		return c;
	}
}
